#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>
#include<string.h>
void* thread1(void* argv)
{
    printf("thread1 is returning....\n");
    int *p=(int*)malloc(sizeof(int));
    *p=1;
    return (void*)p;
}

void* thread2(void* argv)
{
    printf("thread2 is returning....\n");
    int *p=(int*)malloc(sizeof(int));
    *p=2;
    return (void*)p;
}

void* thread3(void* argv)
{
   while(1)
   {
    printf("thread3 is running...\n");
    sleep(1);
   }
   return NULL;
}

//使用pthread_join函数
int main()
{
    pthread_t td;
    int n;
    void * value;

    //加入线程1
    if((pthread_create(&td,NULL,thread1,NULL)!=0))
    {
        perror("pthread_create is error\n");
        exit(1);
    }
    pthread_join(td,&value);
    printf("thread return, thread id %lX, return code:%d\n", td, *(int*)value);
    free(value);

    //加入线程2
    if((pthread_create(&td,NULL,thread2,NULL)!=0))
    {
        perror("pthread_create is error\n");
        exit(1);
    }
    pthread_join(td,&value);
    printf("thread return, thread id %lX, return code:%d\n", td, *(int*)value);
    free(value);

    //加入线程3
    if((pthread_create(&td,NULL,thread3,NULL)!=0))
    {
        perror("pthread_create is error\n");
        exit(1);
    }
    sleep(3);
    //线程3没办法主动退出,将会一直处于挂起状态
    pthread_cancel(td);
    pthread_join(td,&value);
    if(value==PTHREAD_CANCELED)
    {
        printf("thread return ,thread id is %lX,return code:PTHREAD_CANCELED\n",td);
    }
    return 0;
}